package com.lxs.doc.rocess.common;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;

import java.io.FileInputStream;
import java.io.IOException;

public class DocOutlineParser {
    public static void parseDocOutline(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath);
             HWPFDocument document = new HWPFDocument(fis)) {

            // 获取文档范围
            Range range = document.getRange();

            // 解析段落大纲
            parseParagraphsOutline(range);

            // 解析样式大纲
            parseStylesOutline(document);

            // 解析书签大纲（如果有）
            parseBookmarksOutline(document);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void parseParagraphsOutline(Range range) {
        System.out.println("=== 段落大纲 ===");

        for (int i = 0; i < range.numParagraphs(); i++) {
            Paragraph paragraph = range.getParagraph(i);

            // 获取段落文本
            String text = paragraph.text().trim();
            if (text.isEmpty()) continue;

            // 获取段落样式信息
            int styleIndex = paragraph.getStyleIndex();
            int paragraphLevel = paragraph.getLvl();
            boolean isHeading = isHeadingStyle(paragraph);

            System.out.printf("段落 %d [级别: %d, 样式: %d, 标题: %s]%n",
                    i, paragraphLevel, styleIndex, isHeading);
            System.out.println("内容: " + text);
            System.out.println("---");
        }
    }

    private static void parseStylesOutline(HWPFDocument document) {
        System.out.println("=== 样式大纲 ===");

        StyleSheet styleSheet = document.getStyleSheet();
        if (styleSheet != null) {
            for (int i = 0; i < styleSheet.numStyles(); i++) {
                StyleDescription style = styleSheet.getStyleDescription(i);
                if (style != null) {
                    System.out.printf("样式 %d: %s (基准样式: %d)%n",
                            i, style.getName(), style.getBaseStyle());

                    // 输出样式详细信息
                    if (style.getPAP() != null) {
                        System.out.printf("  段落级别: %d%n", style.getPAP().getIlvl());
                    }
                }
            }
        }
    }

    private static void parseBookmarksOutline(HWPFDocument document) {
        System.out.println("=== 书签大纲 ===");

        Bookmarks bookmarks = document.getBookmarks();
        if (bookmarks != null) {
            for (int i = 0; i < bookmarks.getBookmarksCount(); i++) {
                Bookmark bookmark = bookmarks.getBookmark(i);
                System.out.printf("书签: %s (开始: %d, 结束: %d)%n",
                        bookmark.getName(), bookmark.getStart(), bookmark.getEnd());
            }
        }
    }

    private static boolean isHeadingStyle(Paragraph paragraph) {
        // 判断是否为标题样式（简单判断）
        int styleIndex = paragraph.getStyleIndex();
        String text = paragraph.text().trim();

        // 根据样式索引或文本特征判断
        return styleIndex >= 1 && styleIndex <= 9 || // 通常标题样式在1-9
                text.length() < 100 && !text.contains("。") && // 标题通常较短
                        paragraph.getLvl() > 0; // 有缩进级别
    }
}
